package com.guigu.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.eduservice.entity.PurchaseDemand;
import com.guigu.eduservice.service.PurchaseDemandService;
import com.guigu.eduservice.utils.EmployeeJwtUtils;
import com.guigu.eduservice.vo.PurchaseDemandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 求购信息表（存储买家发布的二手机械求购需求） 前端控制器
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/eduservice/purchaseDemand")
public class PurchaseDemandController {

    @Autowired
    private PurchaseDemandService purchaseDemandService;

    /**
     * 获取求购信息列表
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @PostMapping("getPurchaseDemandPageList/{pageNum}/{pageSize}")
    public PageInfo<PurchaseDemand> getPurchaseDemandPageList(@PathVariable Integer pageNum,
                                                              @PathVariable Integer pageSize,
                                                              @RequestBody PurchaseDemandVo vo) {
        return purchaseDemandService.getPurchaseDemandPageList(pageNum, pageSize, vo);
    }

    @GetMapping("/getPurchaseDemandByUserId/{userId}")
    public R getPurchaseDemandByUserId(@PathVariable Long userId) {
        List<PurchaseDemand> list = purchaseDemandService.getPurchaseDemandByUserId(userId);
        return R.ok().data("purchaseDemandList", list);
    }
    @PostMapping("/addPurchaseDemand")
    public R addPurchaseDemand(@RequestBody PurchaseDemand purchaseDemand) {
        purchaseDemand.setReleaseTime(LocalDateTime.now());
        purchaseDemand.setPurchaseStatus(0);
        boolean save = purchaseDemandService.save(purchaseDemand);
        return save ? R.ok() : R.error();
    }
    @DeleteMapping("/deletePurchaseDemand/{purchaseId}")
    public R deletePurchaseDemand(@PathVariable Long purchaseId) {
        boolean remove = purchaseDemandService.removeById(purchaseId);
        return remove ? R.ok() : R.error();
    }
    @PostMapping("/updatePurchaseDemand")
    public R updatePurchaseDemand(@RequestBody PurchaseDemand purchaseDemand) {
        boolean update = purchaseDemandService.updateById(purchaseDemand);
        return update ? R.ok() : R.error();
    }
    /**
     * 分配业务员id
     * @param purchaseDemand
     * @return
     */
    @PostMapping("updatePurchaseDemandByBusinessmanId")
    public boolean updatePurchaseDemandByBusinessmanId(@RequestBody PurchaseDemand purchaseDemand) {
        return purchaseDemandService.updatePurchaseDemandByBusinessmanId(purchaseDemand.getPurchaseId(),purchaseDemand.getBusinessmanId());
    }

    //查询员工id绑定的求购信息()
    @GetMapping("/selectBusinessId")
    public R selectBusinessId(HttpServletRequest request){
        int businessmanId = EmployeeJwtUtils.getUserIDByJwtToken(request);
        QueryWrapper<PurchaseDemand> wrapper = new QueryWrapper<>();
        wrapper.eq("purchase_status", 1);
        wrapper.eq("businessman_id", businessmanId);
        List<PurchaseDemand> list = purchaseDemandService.list(wrapper);
        return  R.ok().data("list", list);
    }
    //修改状态
    @GetMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable long id){
        PurchaseDemand byId = purchaseDemandService.getById(id);
        byId.setPurchaseStatus(2);
        boolean update = purchaseDemandService.updateById(byId);
        if (update){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }
}
